package com.lmy.springboot_quartz;

import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.PersistJobDataAfterExecution;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.Date;

/**
 * 定义一个定时任务类
 */
//禁止定时任务并发执行，严格按照任务实际执行时间顺序执行
@DisallowConcurrentExecution
//将JobDetail的JobDataMap持久化
@PersistJobDataAfterExecution
public class QuartzJob extends QuartzJobBean {

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        try {
            System.out.println("=====QuartzJob execute.....======");
            Thread.sleep(2000);
            //输出调度器实例，为同一个实例，因为已将Scheduler通过配置类的方式注入spring容器，是单例的
            System.out.println(jobExecutionContext.getScheduler().getSchedulerInstanceId());
            System.out.println("taskname=" + jobExecutionContext.getJobDetail().getKey().getName());
            System.out.println("执行时间=" + new Date());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
